<template>
  <div class="modal-overlay" @click.self="close">
    <div class="modal-container">
      <div class="modal-header">
        <h3></h3>
        <button class="modal-close" @click="close">
          <svg viewBox="0 0 24 24" width="20" height="20">
            <path fill="currentColor"
              d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" />
          </svg>
        </button>
      </div>

      <div class="modal-body">
        <form @submit.prevent="save">
          <div class="form-group">
            <label>用户名</label>
            <input type="text" v-model="formData.name" required placeholder="输入用户名">
          </div>

          <div class="form-group">
            <label>电子邮箱</label>
            <input type="email" v-model="formData.email" required placeholder="输入电子邮箱">
          </div>

          <div class="form-group">
            <label>用户角色</label>
            <select v-model="formData.role" required>
              <option v-for="role in roles" :value="role.value" :key="role.value">
                {{ role.label }}
              </option>
            </select>
          </div>

          <div class="form-group">
            <label>用户状态</label>
            <select v-model="formData.status" required>
              <option value="active">活跃</option>
              <option value="inactive">未激活</option>
              <option value="banned">已封禁</option>
            </select>
          </div>

          <div class="form-group">
            <label>头像URL</label>
            <input type="url" v-model="formData.avatar" placeholder="输入头像URL">
            <div class="avatar-preview" v-if="formData.avatar">
              <img :src="formData.avatar" alt="头像预览">
            </div>
          </div>

          <div class="form-actions">
            <button type="button" class="btn secondary" @click="close">
              取消
            </button>
            <button type="submit" class="btn primary">
              保存
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  user: {
    type: Object,
    default: null
  },
  roles: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['save', 'close'])

const defaultFormData = {
  name: '',
  email: '',
  role: 'user',
  status: 'active',
  avatar: 'https://randomuser.me/api/portraits/lego/5.jpg'
}

const formData = ref({ ...defaultFormData })

// 当props.user变化时更新表单数据
watch(() => props.user, (newUser) => {
  if (newUser) {
    formData.value = { ...newUser }
  } else {
    formData.value = { ...defaultFormData }
  }
}, { immediate: true })

const save = () => {
  emit('save', formData.value)
}

const close = () => {
  emit('close')
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-container {
  background-color: var(--card-bg);
  border-radius: 8px;
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

.modal-header {
  padding: 1.5rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.2rem;
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 0.3rem;
  border-radius: 4px;
}

.modal-close:hover {
  background-color: var(--hover-bg);
}

.modal-body {
  padding: 1.5rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 0.5rem 0.8rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background-color: var(--card-bg);
  color: var(--text-color);
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--primary-color);
}

.avatar-preview {
  margin-top: 1rem;
}

.avatar-preview img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  margin-top: 2rem;
}

.btn {
  padding: 0.5rem 1.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s ease;
}

.btn.secondary {
  border: 1px solid var(--border-color);
  background-color: var(--card-bg);
  color: var(--text-color);
}

.btn.secondary:hover {
  background-color: var(--hover-bg);
}

.btn.primary {
  border: none;
  background-color: var(--primary-color);
  color: white;
}

.btn.primary:hover {
  background-color: var(--primary-color);
  opacity: 0.9;
}
</style>